import random

from django_redis import get_redis_connection
from rest_framework.response import Response
from rest_framework.views import APIView
from app.ser import UserSer,PostSer
from app.models import User,Post
from django.core.cache import cache
from django.core.paginator import Paginator


# Create your views here.
class Users(APIView):
    def post(self,request):
        username = request.data.get('username')
        email = request.data.get('email')
        password = request.data.get('password')
        try:
            user=User.objects.create(username=username,email=email,password=password)
            user1 = UserSer(instance=user)

        except Exception as e :
            print(e)
            return Response({'code':400,'msg':'数据库连接失败'})

        return Response({'code':200,'msg':'存入成功','data':user1.data})





class Postcon(APIView):
    def post(self,request):
        title = request.data.get('title')
        content = request.data.get('content')
        user = request.data.get('user')
        try:
            p1 = Post.objects.create(title=title,content=content,user_id=user)
            pser = PostSer(instance=p1)
        except Exception as e:
            print(e)
            return Response({'msg':'数据库连接失败'})


        return Response({'code':200,'msg':'存入成功','data':pser.data})


class SMSCodeView(APIView):
    def get(self,request):
        redis = get_redis_connection('verify_code')
        # 9.发送短信验证码
        sms_code = random.randint(1000, 9999)
        redis.setex('sms', 300, sms_code)
        from qomokao.celery_tasks.sms.tasks import send_sms
        send_sms.delay(sms_code)
        print('短信验证码：', sms_code)
        return Response({'code':200,'msg':'celery调用成功'})





class Fanxiang(APIView):
    def get(self,request,id):
        data = cache.get('data')
        if data:


            user = User.objects.get(id=id)
            print('从redis缓存查询')
            print(user)


        p1 = Post.objects.filter(user=id)
        print(p1)

        from django.db import connection
        sql1 = "select * from post order by time desc"
        with connection.cursor() as cursor:
            cursor.execute(sql1)
            row = cursor.fetchall()
            list = []
            for item in row:
                list.append(item)
            print(list)
            cache.sexex('data', list, 60 * 60 * 15)

        return Response({'code': 200, 'msg': '查询成功', 'data': list})

class Postss(APIView):
    def get(self,request):
        current_page = request.query_params.get("p")  # 当前第几页


        po = Post.objects.all()

        paginator = Paginator(po,4)  # 分页器对象
        page_dangqian = paginator.page(current_page)  # 分页之后的数据集对象
        serpo = PostSer(instance=page_dangqian,many=True)

        return Response({'code':200,'data':serpo.data})


class Sql(APIView):
    def get(self,request,id):


        from django.db import connection
        sql1 = "select * from post group by id limit 10"
        with connection.cursor() as cursor:
            cursor.execute(sql1)
            row = cursor.fetchall()
            list = []
            for item in row:
                list.append(item)
            print(list)


        return Response({'code': 200, 'msg': '查询成功', 'data': list})

